package org.jetnuke.modules.news;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.dao.UserDAO;
import net.jforum.entities.User;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.StoriesDAO;
import org.jetnuke.entities.Comments;
import org.jetnuke.entities.Stories;

public class Story implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Story.class);

	int id;

	public void setId(int id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		StoriesDAO md = DataAccessDriver.getInst().newStoriesDAO();
		Stories st = md.retrieveByPK(id);
		md.incCounter(id);
		HashMap userMap = new HashMap();
		vc.put("userMap", userMap);
		UserDAO um = DataAccessDriver.getInst().newUserDAO();
		User user = um.selectById(st.getAID());
		userMap.put(new Integer(st.getAID()), user);
		if (st.getINFORMANT() >= 2) {
			user = um.selectById(st.getINFORMANT());
			userMap.put(new Integer(st.getINFORMANT()), user);
		}

		// stories st = storiesPeer.retrieveByPK(id);
		// st.setCOUNTER(st.getCOUNTER() + 1);
		vc.put("topic", DataAccessDriver.getInst().newTopicsDAO().retrieveByPK(
				st.getTOPIC()));
		// md.update(st);
		vc.put("story", st);
		List level0 = DataAccessDriver.getInst().newCommentDAO()
				.selectBySID_PID(id, 0);
		// commentsPeer.doSelect(new Criteria().add(
		// commentsPeer.SID, id).add(commentsPeer.PID, 0));
		Iterator it = level0.iterator();
		while (it.hasNext()) {
			Comments comm = (Comments) it.next();
			fillKids(comm, userMap);
		}
		vc.put("comments", level0);
		Stories topStory = DataAccessDriver.getInst().newStoriesDAO()
				.getTopByTopic(st.getTOPIC());

		// storiesPeer.doSelect(new Criteria().add(storiesPeer.TOPIC,
		// st.getTOPIC()).addDescendingOrderByColumn(storiesPeer.COUNTER)
		// .setLimit(1));
		vc.put("topstory", topStory);

		int score = 0;
		if (st.getRATINGS() > 0)
			score = (int) (((double) st.getSCORE()) / st.getRATINGS());
		vc.put("rate", new Integer(score));
		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

	private void fillKids(Comments comm, HashMap userMap) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("fillKids(Comments, HashMap) - start"); //$NON-NLS-1$
		}

		UserDAO um = DataAccessDriver.getInst().newUserDAO();
		List ls = DataAccessDriver.getInst().newCommentDAO().selectBySID_PID(
				id, comm.getTID());
		// commentsPeer
		// .doSelect(new Criteria().add(commentsPeer.SID, id).add(
		// commentsPeer.PID, comm.getTID()));
		comm.setKids(ls);
		Iterator it = ls.iterator();
		while (it.hasNext()) {
			Comments comm_ = (Comments) it.next();
			int uid = comm_.getNAME();
			Integer itn = new Integer(uid);
			if (!userMap.containsKey(itn)) {
				User user = um.selectById(uid);
				userMap.put(itn, user);
			}
			fillKids(comm_, userMap);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("fillKids(Comments, HashMap) - end"); //$NON-NLS-1$
		}
	}

}
